﻿using System;
using System.Windows;

using Coding4Fun.Cannon.Phone.Accelerometer;
using Coding4Fun.Cannon.Phone.Service;

namespace Coding4Fun.Cannon.Phone
{
    public partial class App : Application
    {
        //Static Globals
		public static ConcreteAccelerometer Accelerometer = new ConcreteAccelerometer();
		public static CannonService Service;
		public static Settings ApplicationSettings = new Settings();

        public App()
        {
            UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(Application_UnhandledException);

            InitializeComponent();

			ReloadSettings();
        }

		public static void ReloadSettings()
		{
			ApplicationSettings = Settings.Load();
			Service = new CannonService(ApplicationSettings);
		}

        // Code to execute on Unhandled Exceptions
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            Logger.WriteLine(e.ExceptionObject.Message, MessageType.Error); 
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // An unhandled exception has occurred, break in the debugger
                System.Diagnostics.Debugger.Break();
            }
            else
            {
                // By default show the error
                e.Handled = true;
                MessageBox.Show(e.ExceptionObject.Message + Environment.NewLine + e.ExceptionObject.StackTrace,
                    "Error", MessageBoxButton.OK);
            }
        }
    }
}